package dao.userdao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import util.SHAUtil;

import dao.basedao.DBManager;
import model.User;

public class UserDaoImpl implements UserDao {

	@Override
	public int queryAccount(User user) throws Exception, ClassNotFoundException, Exception {
		int flag=0;
		Connection con=DBManager.getConn();
		PreparedStatement ps=con.prepareStatement("SELECT user.user_id,user.password,user_info.name AS username,user_info.role_id AS role_id FROM user INNER JOIN user_info  ON user.user_id = user_info.user_id where user.user_id=? and user.password=?");
        ps.setString(1,user.getUser_id());
        ps.setString(2, SHAUtil.shaEncode(user.getPassword()));
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
        	user.setRole_id(rs.getInt("role_id"));
        	user.setUsername(rs.getString("username"));
        }
		flag=user.getRole_id();
		rs.close();
		ps.close();
		DBManager.closeConn(con);
		return flag;
	}
    public void addUser(User user)throws Exception{
		Connection con=DBManager.getConn();
		PreparedStatement ps=con.prepareStatement("insert into user(user_id,password) values(?,?)");
		ps.setString(1, user.getUser_id());
		ps.setString(2, SHAUtil.shaEncode(user.getPassword()));
		ps.executeUpdate();
		ps.close();
		DBManager.closeConn(con);
    }
}
